package com.dchh.android.suverynew.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dchh.android.suverynew.R;

import java.util.List;
import java.util.Map;

/**
 * 层级列表
 *
 * @author zhoudan
 * @date 2015年12月30日
 */
public class LevelSelectAdapter extends BaseAdapter {

    private Activity activity;

    private List<Map<String, String>> data;

    private int select = -1;

    private boolean iconFlag = false;

    public LevelSelectAdapter(Activity activity, List<Map<String, String>> data) {
        this.activity = activity;
        this.data = data;
    }

    public void setSelect(int select) {
        this.select = select;
    }

    public void setIconFlag(boolean iconFlag) {
        this.iconFlag = iconFlag;
    }

    public void refresh(List<Map<String, String>> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Map<String, String> getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup arg2) {
        View row = v;
        ViewHolder holder;
        if (row == null) {
            holder = new ViewHolder();
            row = LayoutInflater.from(activity).inflate(R.layout.util_map_point_list_item, null);
            holder.id = (TextView) row.findViewById(R.id.point_id);
            holder.icon = (ImageView) row.findViewById(R.id.start_icon);
            holder.name = (TextView) row.findViewById(R.id.point_name);

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        Map<String, String> map = getItem(position);

        if (iconFlag) {
            if (map.get("icon").equals("1")) {
                holder.icon.setVisibility(View.VISIBLE);
            } else {
                holder.icon.setVisibility(View.INVISIBLE);
            }
        } else {
            holder.icon.setVisibility(View.GONE);
        }

        holder.id.setText(map.get("id"));
        holder.name.setText(map.get("name"));

        if (position == select) {
            row.setBackgroundResource(R.drawable.btn_blue_selector);
            holder.name.setTextColor(activity.getResources().getColor(
                    R.color.white));
        } else {
            row.setBackgroundResource(R.drawable.item_border_0_blue_selector);
            holder.name.setTextColor(activity.getResources().getColor(
                    R.color.font_deep_gray));
        }

        return row;
    }

    public final class ViewHolder {
        public TextView id;
        public ImageView icon;
        public TextView name;
    }
    
    /*public interface OnLevelSelectListener{
        public void onSelect(int position);
    }*/
}
